/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.Warehouse;

import entity.AccessRole;
import entity.BatchRequired;
import entity.Employee;
import entity.Inventory;
import entity.Job;
import entity.ProductionBatch;
import entity.ProductionScheduleDay;
import entity.PurchaseItem;
import entity.TrackingItem;
import entity.Warehouse;
import entity.WarehouseZone;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import managedbean.UserSessionManagerBean;
import session.stateless.AccessRoleSessionBeanLocal;
import session.stateless.EmployeeSessionBeanLocal;
import session.stateless.JobSessionBeanLocal;
import session.stateless.LogSessionBeanLocal;
import session.stateless.MessageSessionBeanLocal;
import session.stateless.ProductionBatchSessionBeanLocal;
import session.stateless.ProductionScheduleSessionBeanLocal;
import session.stateless.PurchaseOrderSessionBeanLocal;
import session.stateless.WarehouseSessionBeanLocal;

/**
 *
 * @author Wx
 */
@ManagedBean(name = "warehouseManagerBean")
@RequestScoped
public class WarehouseManagerBean implements Serializable {

    @EJB
    WarehouseSessionBeanLocal warehouseSessionBeanLocal;
    @EJB
    ProductionBatchSessionBeanLocal productionBatchSessionBeanLocal;
    @EJB
    PurchaseOrderSessionBeanLocal purchaseOrderSessionBeanLocal;
    @EJB
    ProductionScheduleSessionBeanLocal productionScheduleSessionBeanLocal;
    @EJB
    LogSessionBeanLocal logSessionBeanLocal;
    @EJB
    EmployeeSessionBeanLocal employeeSessionBeanLocal;
    @EJB
    MessageSessionBeanLocal messageSessionBeanLocal;
    @EJB
    JobSessionBeanLocal jobSessionBeanLocal;
    @EJB
    AccessRoleSessionBeanLocal accessRoleSessionBeanLocal;
    @ManagedProperty(value = "#{userSessionManagerBean}")
    private UserSessionManagerBean userSessionManagerBean;
    private String loadManageWarehouses;
    private List<Warehouse> warehouses = new ArrayList<Warehouse>();
    private Warehouse selectedWarehouse;
    private String loadUpdateWarehouse;
    private List<WarehouseZone> warehouseZones = new ArrayList<WarehouseZone>();
    private List<WarehouseZone> warehouseZones2 = new ArrayList<WarehouseZone>();
    private WarehouseZone warehouseZone = new WarehouseZone();
    private String loadAddWarehouseZone;
    private WarehouseZone selectedWarehouseZone;
    private String loadManageWarehouseZones;
    private String loadUpdateWarehouseZone;
    //WarehouseZone
    private String zoneName;
    private String type;
    private Double quantityLimit;
    private Double currentQuantity;
    private String request;
    private Boolean isUpdate = true;
    private Boolean notUpdate = true;
    private ProductionBatch productionBatch;
    private Double totalWt;

    /** Creates a new instance of WarehouseManagerBean */
    public WarehouseManagerBean() {
    }

    public void deleteZoneFromWarehouse(ActionEvent event) {
        try {
            if (selectedWarehouseZone != null) {
                warehouseSessionBeanLocal.deleteZoneFromWarehouse(selectedWarehouse.getId(), selectedWarehouseZone.getId());

                selectedWarehouse = warehouseSessionBeanLocal.getWarehouse(selectedWarehouse.getId());

                userSessionManagerBean.setSelectedWarehouse(selectedWarehouse);

                FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                        + getUserSessionManagerBean().getServerName() + ":"
                        + getUserSessionManagerBean().getServerPort()
                        + "/MerlionERP-war/Web_Warehouse/updateWarehouse.xhtml"); //redirect to updateWarehouse.xhtml
            } else {
                FacesContext.getCurrentInstance().addMessage(null,
                        new FacesMessage(FacesMessage.SEVERITY_ERROR, "Please select a zone to delete before continuing.", ""));
            }
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.toString(), ""));
        }
    }

    public void addZone(ActionEvent event) {
        try {
            if (warehouseZone.getId() != null) {
                List<WarehouseZone> temp = selectedWarehouse.getWarehouseZones();

                Boolean exist = false;
                for (WarehouseZone wz : temp) {
                    if (wz.getId().compareTo(warehouseZone.getId()) == 0) {
                        exist = true; //zone to add already is already in the warehouse's zone list

                        break;
                    }
                }

                if (!exist) { //if zone to add does not exist in the warehouse's zone list
                    warehouseZone = warehouseSessionBeanLocal.getWarehouseZone(warehouseZone.getId());

                    warehouseSessionBeanLocal.updateWarehouse(selectedWarehouse.getId(), warehouseZone);

                    selectedWarehouse = warehouseSessionBeanLocal.getWarehouse(selectedWarehouse.getId());

                    userSessionManagerBean.setSelectedWarehouse(selectedWarehouse);

                    FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                            + getUserSessionManagerBean().getServerName() + ":"
                            + getUserSessionManagerBean().getServerPort()
                            + "/MerlionERP-war/Web_Warehouse/updateWarehouse.xhtml"); //redirect to updateWarehouse.xhtml
                } else { //if zone to add already exists in the warehouse's zone list
                    FacesContext.getCurrentInstance().addMessage(null,
                            new FacesMessage(FacesMessage.SEVERITY_ERROR, "The zone to add already exists in the warehouse's zone list.", ""));
                }
            } else {
                FacesContext.getCurrentInstance().addMessage(null,
                        new FacesMessage(FacesMessage.SEVERITY_ERROR, "Please select a zone to add.", ""));
            }
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.toString(), ""));
        }
    }

    public void updateAWarehouseZone(ActionEvent event) {
        try {
            if (type != null) {
                WarehouseZone temp = warehouseSessionBeanLocal.getWarehouseZoneByName(zoneName, selectedWarehouseZone.getId());

                if (temp == null) {
                    selectedWarehouseZone.setCurrentQuantity(currentQuantity);
                    selectedWarehouseZone.setQuantityLimit(quantityLimit);
                    selectedWarehouseZone.setZoneName(zoneName);
                    selectedWarehouseZone.setType(type);

                    warehouseSessionBeanLocal.updateWarehouseZone(selectedWarehouseZone);

                    FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                            + getUserSessionManagerBean().getServerName() + ":"
                            + getUserSessionManagerBean().getServerPort()
                            + "/MerlionERP-war/Web_Warehouse/searchWarehouseZones.xhtml"); //redirect to searchWarehouseZones.xhtml
                } else {
                    FacesContext.getCurrentInstance().addMessage(null,
                            new FacesMessage(FacesMessage.SEVERITY_ERROR, "A zone with the same name already exists.", ""));
                }
            } else {
                FacesContext.getCurrentInstance().addMessage(null,
                        new FacesMessage(FacesMessage.SEVERITY_ERROR, "Please select a type for the zone.", ""));
            }
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.toString(), ""));
        }
    }

    public void updateWarehouseZone(ActionEvent event) {
        try {
            if (selectedWarehouseZone != null) {
                getUserSessionManagerBean().setSelectedWarehouseZone(selectedWarehouseZone);

                FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                        + getUserSessionManagerBean().getServerName() + ":"
                        + getUserSessionManagerBean().getServerPort()
                        + "/MerlionERP-war/Web_Warehouse/updateWarehouseZone.xhtml"); //redirect to updateWarehouseZone.xhtml
            } else {
                FacesContext.getCurrentInstance().addMessage(null,
                        new FacesMessage(FacesMessage.SEVERITY_ERROR, "Please select a warehosue zone before continuing.", ""));
            }
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.toString(), ""));
        }
    }

    public void addWarehouseZone(ActionEvent event) {
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                    + getUserSessionManagerBean().getServerName() + ":"
                    + getUserSessionManagerBean().getServerPort()
                    + "/MerlionERP-war/Web_Warehouse/addWarehouseZone.xhtml"); //redirect to addWarehouseZone.xhtml
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.toString(), ""));
        }
    }

    public void addAWarehouseZone(ActionEvent event) {
        try {
            if (type != null) {
                WarehouseZone temp = warehouseSessionBeanLocal.getWarehouseZone(zoneName);

                if (temp == null) {
                    WarehouseZone warehouseZone = new WarehouseZone(zoneName, type, quantityLimit, currentQuantity);

                    warehouseSessionBeanLocal.addWarehouseZone(warehouseZone);

                    FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                            + getUserSessionManagerBean().getServerName() + ":"
                            + getUserSessionManagerBean().getServerPort()
                            + "/MerlionERP-war/Web_Warehouse/searchWarehouseZones.xhtml"); //redirect to searchWarehouseZones.xhtml
                } else {
                    FacesContext.getCurrentInstance().addMessage(null,
                            new FacesMessage(FacesMessage.SEVERITY_ERROR, "A zone with the same name already exists.", ""));
                }
            } else {
                FacesContext.getCurrentInstance().addMessage(null,
                        new FacesMessage(FacesMessage.SEVERITY_ERROR, "Please select a type for the zone.", ""));
            }
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.toString(), ""));
            ex.printStackTrace();
        }
    }

    public void back(ActionEvent event) {
        try {
            if (event.getComponent().getId().equals("btnBackFromAddZone")) {
                FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                        + getUserSessionManagerBean().getServerName() + ":"
                        + getUserSessionManagerBean().getServerPort()
                        + "/MerlionERP-war/Web_Warehouse/searchWarehouseZones.xhtml"); //redirect to searchWarehouseZones.xhtml
            } else if (event.getComponent().getId().equals("btnBackFromUpdateZone")) {
                FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                        + getUserSessionManagerBean().getServerName() + ":"
                        + getUserSessionManagerBean().getServerPort()
                        + "/MerlionERP-war/Web_Warehouse/searchWarehouseZones.xhtml"); //redirect to searchWarehouseZones.xhtml
            } else if (event.getComponent().getId().equals("btnBackFromUpdateWarehouse")) {
                FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                        + getUserSessionManagerBean().getServerName() + ":"
                        + getUserSessionManagerBean().getServerPort()
                        + "/MerlionERP-war/Web_Warehouse/searchWarehouses.xhtml"); //redirect to searchWarehouses.xhtml
            }
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.toString(), ""));
            ex.printStackTrace();
        }
    }

    public void updateWarehouse(ActionEvent event) {
        try {
            if (selectedWarehouse != null) {
                userSessionManagerBean.setSelectedWarehouse(selectedWarehouse);

                FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                        + getUserSessionManagerBean().getServerName() + ":"
                        + getUserSessionManagerBean().getServerPort()
                        + "/MerlionERP-war/Web_Warehouse/updateWarehouse.xhtml"); //redirect to updateWarehouse.xhtml
            } else {
                FacesContext.getCurrentInstance().addMessage(null,
                        new FacesMessage(FacesMessage.SEVERITY_ERROR, "Please select a warehouse before continuing.", ""));
            }
        } catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.toString(), ""));
        }
    }

    public void updateProductionBatch(ActionEvent event) {
//        setProductionBatch((ProductionBatch) userSessionManagerBean.getSelectedItem());
//        Double weight = getProductionBatch().getQuantity() * getProductionBatch().getProduct().getWeight();
        warehouseZone = selectedWarehouseZone;
        if (warehouseZone != null) {
            if (warehouseZone.getQuantityLimit() - warehouseZone.getCurrentQuantity() >= totalWt) {
                Inventory inventory = warehouseSessionBeanLocal.getInventoryByProduct(getProductionBatch().getProduct().getName());
                Double bars = inventory.getQuantity();
                bars += getProductionBatch().getQuantity();
                inventory.setQuantity(bars);
                totalWt += warehouseZone.getCurrentQuantity();
                warehouseZone.setCurrentQuantity(totalWt);
                productionBatchSessionBeanLocal.updateProductionBatchZone(getProductionBatch().getId(), warehouseZone.getId());
                warehouseSessionBeanLocal.updateInventory(inventory);
                TrackingItem ti = new TrackingItem();
                ti.setDescription("" + getProductionBatch().getQuantity());
                warehouseSessionBeanLocal.updateInventory(inventory, ti);
                warehouseSessionBeanLocal.updateWarehouseZone(warehouseZone);
                warehouseSessionBeanLocal.updateWarehouse(Long.parseLong("1000"), getProductionBatch());
                Warehouse warehouse = warehouseSessionBeanLocal.getWarehouse(Long.parseLong("1000"));
                ArrayList<ProductionBatch> pbs = new ArrayList(warehouse.getProductionBatches());
                ArrayList<PurchaseItem> pois = new ArrayList<PurchaseItem>();
                HashMap hashPoi = new HashMap();
                for (ProductionBatch pb : pbs) {
                    for (PurchaseItem poi : pb.getProductionScheduleDay().getPurchaseItems()) {

                        if (hashPoi.get(poi) == null && poi.getStatus().compareTo("Pending for Shipping") == 0) {
                            hashPoi.put(poi, 0);
                            pois.add(poi);
                        }
                    }
                }


                //loop through inventory to satisfy shipping
                for (PurchaseItem poi : pois) {
                    for (ProductionBatch pb : pbs) {

                        if (poi.getProduct().getCode().compareTo(getProductionBatch().getProduct().getCode()) == 0) {

                            int count = 0;
                            for (BatchRequired br : poi.getBatchesRequired()) {
                                count += br.getQuantity();
                            }

                            if (pb.getQuantity() - pb.getUtilizedQuantity() > 0 && poi.getStatus().compareTo("Pending for Shipping") == 0) {
                                if (poi.getQuantity() - count <= pb.getQuantity() - pb.getUtilizedQuantity()) {
                                    count = poi.getQuantity() - count;
                                    BatchRequired br = new BatchRequired(count, pb);
                                    poi.getBatchesRequired().add(br);
                                    pb.setUtilizedQuantity(pb.getUtilizedQuantity() + count);
                                    poi.setStatus("Ready for Shipping");

                                } else {
                                    BatchRequired br = new BatchRequired(pb.getQuantity() - pb.getUtilizedQuantity(), pb);
                                    poi.getBatchesRequired().add(br);
                                    pb.setUtilizedQuantity(pb.getUtilizedQuantity() + (pb.getQuantity() - pb.getUtilizedQuantity()));

                                    
                                    Employee owner = employeeSessionBeanLocal.getEmployee("system");
                                    messageSessionBeanLocal.addMessage("System", userSessionManagerBean.getUserId(), "Safety buffer exception",
                                            "Safety stock unable to fulfill this order."
                                            + "<p><hr/><p>This is a system generated message, no reply is required.", owner);

                                    owner = employeeSessionBeanLocal.getEmployee(userSessionManagerBean.getUserId());
                                  messageSessionBeanLocal.addMessage("System", userSessionManagerBean.getUserId(), "Safety buffer exception",
                                            "Safety stock unable to fulfill this order."
                                            + "<p><hr/><p>This is a system generated message, no reply is required.", owner);

                                }
                            }
                        }
                    }

                }

                //Update status for poi that is ready for shipping
                for (PurchaseItem poi : pois) {
                    for (BatchRequired br : poi.getBatchesRequired()) {
                        purchaseOrderSessionBeanLocal.addPurchaseItemBatch(poi.getId(), br);
                    }
                    if (poi.getStatus().compareTo("Ready for Shipping") == 0) {
                        purchaseOrderSessionBeanLocal.updatePurchaseItemStatus(poi.getId(), poi.getStatus());


                        Job j = jobSessionBeanLocal.getJob(poi);
                        Employee owner = employeeSessionBeanLocal.getEmployee("system");
                        messageSessionBeanLocal.addMessage("System", j.getEmployee().getUserId(), "Job delivery information",
                                "Purchase item for job:" + j.getJobId() + " is ready for delivery. No follow up action require."
                                + "<p><hr/><p>This is a system generated message, no reply is required.", owner);

                        owner = j.getEmployee();
                        messageSessionBeanLocal.addMessage("System", j.getEmployee().getUserId(), "Job delivery information",
                                "Purchase item for job:" + j.getJobId() + " is ready for delivery. No follow up action require."
                                + "<p><hr/><p>This is a system generated message, no reply is required.", owner);


                        ArrayList<AccessRole> rlist = accessRoleSessionBeanLocal.getAccessRoles("Add Production Schedule");
                        ArrayList<Employee> employees = employeeSessionBeanLocal.getEmployees(rlist);

                        for (Employee em : employees) {
                            owner = employeeSessionBeanLocal.getEmployee("System");
                            messageSessionBeanLocal.addMessage("System", em.getUserId(), "Sales order ready for shipping",
                                    "One item is ready for shipping. Please access the shipping page to ship out this order."
                                    + "<p><hr/><p>This is a system generated message, no reply is required.", owner);

                            owner = employeeSessionBeanLocal.getEmployee(em.getUserId());
                            messageSessionBeanLocal.addMessage("System", em.getUserId(), "Sales order ready for shipping",
                                    "One item is ready for shipping. Please access the shipping page to ship out this order."
                                    + "<p><hr/><p>This is a system generated message, no reply is required.", owner);
                        }


                    } else {
                        Calendar temp = Calendar.getInstance();
                        temp.setTime(poi.getRequestDate());
                        Calendar cal = Calendar.getInstance();
                        cal.add(Calendar.DAY_OF_MONTH, +7);

                        if (cal.after(temp)) {
                            for (BatchRequired br : poi.getBatchesRequired()) {
                                for (ProductionBatch pb : pbs) {
                                    if (br.getProductionBatch().getId().compareTo(pb.getId()) == 0) {
                                        pb.setUtilizedQuantity(pb.getUtilizedQuantity() - br.getQuantity());

                                    }
                                }
                            }

                            ArrayList<ProductionScheduleDay> psds = productionScheduleSessionBeanLocal.getProductionScheduleDays("Backorder");
                            Boolean found = false;
                            if (psds != null) {

                                for (ProductionScheduleDay psd : psds) {
                                    if (psd.getProduct().getCode().compareTo(poi.getProduct().getCode()) == 0) {
                                        found = true;
                                        productionScheduleSessionBeanLocal.addPurchaseItemToPSD(psd.getId(), poi.getId(), true);
                                    }
                                }
                            }
                            if (!found) {
                                //Create new backorder record
                                ProductionScheduleDay psd = new ProductionScheduleDay(null, 1, poi.getProduct(), "Backorder", poi.getQuantity(), poi.getQuantity(), 0.0);
                                productionScheduleSessionBeanLocal.addBackOrder(psd);
                            }


                            Job j = jobSessionBeanLocal.getJob(poi);
                            Employee owner = employeeSessionBeanLocal.getEmployee("system");
                            messageSessionBeanLocal.addMessage("System", j.getEmployee().getUserId(), "Job delivery information",
                                    "Purchase item for job:" + j.getJobId() + " is scheduled for back order. Please re-negotiate with the customer for an agreement."
                                    + "<p><hr/><p>This is a system generated message, no reply is required.", owner);

                            owner = j.getEmployee();
                            messageSessionBeanLocal.addMessage("System", j.getEmployee().getUserId(), "Job delivery information",
                                    "Purchase item for job:" + j.getJobId() + " is scheduled for back order. Please re-negotiate with the customer for an agreement."
                                    + "<p><hr/><p>This is a system generated message, no reply is required.", owner);

                            productionScheduleSessionBeanLocal.removePurchaseItemFromPSDs(poi.getId());
                        }
                    }
                }

                for (ProductionBatch pb : pbs) {
                    productionBatchSessionBeanLocal.updateProductionBatch(pb.getId(), pb.getUtilizedQuantity());
                }



                try {

                    ArrayList<String> al = new ArrayList<String>();
                    al.add("Production Batch successfully moved to warehouse");
                    al.add("You have successfully moved a production batch to a warehouse zone. Shipping department will be notified if there is any sales order ready for shipping.");
                    al.add("Click on the link to return to ");
                    al.add("continue");
                    al.add("Web_Warehouse/searchProductionBatch.xhtml");
                    userSessionManagerBean.setSelectedItem(al);


                    logSessionBeanLocal.addLog("Update production batch", "Production batch: " + productionBatch.getId() + " moved to warehouse zone:" + warehouseZone.getZoneName(), "searchWarehouseZones.xhtml", userSessionManagerBean.getUserId());

                    FacesContext.getCurrentInstance().getExternalContext().redirect("http://"
                            + userSessionManagerBean.getServerName() + ":"
                            + userSessionManagerBean.getServerPort()
                            + "/MerlionERP-war/success.xhtml");
                } catch (Exception ex) {
                }






            } else {
                FacesContext.getCurrentInstance().addMessage(null,
                        new FacesMessage(FacesMessage.SEVERITY_ERROR, "Selected warehosue zone is full.", ""));
            }
        } else {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "Please select a warehosue zone before continuing.", ""));
        }
    }

    /**
     * @return the loadManageWarehouses
     */
    public String getLoadManageWarehouses() {
        warehouses = warehouseSessionBeanLocal.getWarehouses();

        return null;
    }

    /**
     * @param loadManageWarehouses the loadManageWarehouses to set
     */
    public void setLoadManageWarehouses(String loadManageWarehouses) {
        this.loadManageWarehouses = loadManageWarehouses;
    }

    /**
     * @return the warehouses
     */
    public List<Warehouse> getWarehouses() {
        return warehouses;
    }

    /**
     * @param warehouses the warehouses to set
     */
    public void setWarehouses(List<Warehouse> warehouses) {
        this.warehouses = warehouses;
    }

    /**
     * @return the selectedWarehouse
     */
    public Warehouse getSelectedWarehouse() {
        return selectedWarehouse;
    }

    /**
     * @param selectedWarehouse the selectedWarehouse to set
     */
    public void setSelectedWarehouse(Warehouse selectedWarehouse) {
        this.selectedWarehouse = selectedWarehouse;
    }

    /**
     * @return the loadUpdateWarehouse
     */
    public String getLoadUpdateWarehouse() {
        selectedWarehouse = userSessionManagerBean.getSelectedWarehouse();

        warehouseZones = selectedWarehouse.getWarehouseZones();

        warehouseZones2 = warehouseSessionBeanLocal.getWarehouseZones();

        return null;
    }

    /**
     * @param loadUpdateWarehouse the loadUpdateWarehouse to set
     */
    public void setLoadUpdateWarehouse(String loadUpdateWarehouse) {
        this.loadUpdateWarehouse = loadUpdateWarehouse;
    }

    /**
     * @return the warehouseZones
     */
    public List<WarehouseZone> getWarehouseZones() {
        return warehouseZones;
    }

    /**
     * @param warehouseZones the warehouseZones to set
     */
    public void setWarehouseZones(List<WarehouseZone> warehouseZones) {
        this.warehouseZones = warehouseZones;
    }

    /**
     * @return the loadAddWarehouseZone
     */
    public String getLoadAddWarehouseZone() {
        return null;
    }

    /**
     * @param loadAddWarehouseZone the loadAddWarehouseZone to set
     */
    public void setLoadAddWarehouseZone(String loadAddWarehouseZone) {
        this.loadAddWarehouseZone = loadAddWarehouseZone;
    }

    /**
     * @return the zoneName
     */
    public String getZoneName() {
        return zoneName;
    }

    /**
     * @param zoneName the zoneName to set
     */
    public void setZoneName(String zoneName) {
        this.zoneName = zoneName;
    }

    /**
     * @return the type
     */
    public String getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(String type) {
        this.type = type;
    }

    /**
     * @return the quantityLimit
     */
    public Double getQuantityLimit() {
        return quantityLimit;
    }

    /**
     * @param quantityLimit the quantityLimit to set
     */
    public void setQuantityLimit(Double quantityLimit) {
        this.quantityLimit = quantityLimit;
    }

    /**
     * @return the currentQuantity
     */
    public Double getCurrentQuantity() {
        return currentQuantity;
    }

    /**
     * @param currentQuantity the currentQuantity to set
     */
    public void setCurrentQuantity(Double currentQuantity) {
        this.currentQuantity = currentQuantity;
    }

    /**
     * @return the selectedWarehouseZone
     */
    public WarehouseZone getSelectedWarehouseZone() {
        return selectedWarehouseZone;
    }

    /**
     * @param selectedWarehouseZone the selectedWarehouseZone to set
     */
    public void setSelectedWarehouseZone(WarehouseZone selectedWarehouseZone) {
        this.selectedWarehouseZone = selectedWarehouseZone;
    }

    /**
     * @return the loadManageWarehouseZones
     */
    public String getLoadManageWarehouseZones() {

        String temp = (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("request");
        if (temp != null) {

            userSessionManagerBean.setRequest((String) temp + ".xhtml");

            try {

                FacesContext.getCurrentInstance().getExternalContext().redirect("http://" + userSessionManagerBean.getServerName() + ":"
                        + userSessionManagerBean.getServerPort() + "/MerlionERP-war/Web_Warehouse/searchWarehouseZones.xhtml");

            } catch (Exception ex) {
            }
        }
        request = (String) userSessionManagerBean.getRequest();
        if (request != null && request.compareTo("searchProductionBatch.xhtml") == 0) {
            ArrayList<WarehouseZone> wzs = new ArrayList(warehouseSessionBeanLocal.getWarehouseZones());

            for (WarehouseZone wz : wzs) {
                if (wz.getType().compareTo("Product") == 0) {
                    warehouseZones.add(wz);

                }
            }



            setProductionBatch((ProductionBatch) userSessionManagerBean.getSelectedItem());
            totalWt = round(productionBatch.getQuantity() * productionBatch.getProduct().getWeight() / 1000, 2);
            notUpdate = false;




        } else {
            isUpdate = false;
            warehouseZones = warehouseSessionBeanLocal.getWarehouseZones();
            selectedWarehouseZone = null;
        }
        return null;
    }

    /**
     * @param loadManageWarehouseZones the loadManageWarehouseZones to set
     */
    public void setLoadManageWarehouseZones(String loadManageWarehouseZones) {
        this.loadManageWarehouseZones = loadManageWarehouseZones;
    }

    /**
     * @return the userSessionManagerBean
     */
    public UserSessionManagerBean getUserSessionManagerBean() {
        return userSessionManagerBean;
    }

    /**
     * @param userSessionManagerBean the userSessionManagerBean to set
     */
    public void setUserSessionManagerBean(UserSessionManagerBean userSessionManagerBean) {
        this.userSessionManagerBean = userSessionManagerBean;
    }

    /**
     * @return the loadUpdateWarehouseZone
     */
    public String getLoadUpdateWarehouseZone() {
        selectedWarehouseZone = userSessionManagerBean.getSelectedWarehouseZone();

        zoneName = selectedWarehouseZone.getZoneName();
        type = selectedWarehouseZone.getType();
        currentQuantity = selectedWarehouseZone.getCurrentQuantity();
        quantityLimit = selectedWarehouseZone.getQuantityLimit();

        return null;
    }

    /**
     * @param loadUpdateWarehouseZone the loadUpdateWarehouseZone to set
     */
    public void setLoadUpdateWarehouseZone(String loadUpdateWarehouseZone) {
        this.loadUpdateWarehouseZone = loadUpdateWarehouseZone;
    }

    /**
     * @return the warehouseZone
     */
    public WarehouseZone getWarehouseZone() {
        return warehouseZone;
    }

    /**
     * @param warehouseZone the warehouseZone to set
     */
    public void setWarehouseZone(WarehouseZone warehouseZone) {
        this.warehouseZone = warehouseZone;
    }

    /**
     * @return the warehouseZones2
     */
    public List<WarehouseZone> getWarehouseZones2() {
        return warehouseZones2;
    }

    /**
     * @param warehouseZones2 the warehouseZones2 to set
     */
    public void setWarehouseZones2(List<WarehouseZone> warehouseZones2) {
        this.warehouseZones2 = warehouseZones2;
    }

    /**
     * @return the isUpdate
     */
    public Boolean getIsUpdate() {
        return isUpdate;
    }

    /**
     * @param isUpdate the isUpdate to set
     */
    public void setIsUpdate(Boolean isUpdate) {
        this.isUpdate = isUpdate;
    }

    /**
     * @return the notUpdate
     */
    public Boolean getNotUpdate() {
        return notUpdate;
    }

    /**
     * @param notUpdate the notUpdate to set
     */
    public void setNotUpdate(Boolean notUpdate) {
        this.notUpdate = notUpdate;
    }

    /**
     * @return the productionBatch
     */
    public ProductionBatch getProductionBatch() {
        return productionBatch;
    }

    /**
     * @param productionBatch the productionBatch to set
     */
    public void setProductionBatch(ProductionBatch productionBatch) {
        this.productionBatch = productionBatch;
    }

    /**
     * @return the totalWt
     */
    public Double getTotalWt() {
        return totalWt;
    }

    /**
     * @param totalWt the totalWt to set
     */
    public void setTotalWt(Double totalWt) {
        this.totalWt = totalWt;
    }

    public double round(double valueToRound, int numberOfDecimalPlaces) {
        double multiplicationFactor = Math.pow(10, numberOfDecimalPlaces);
        double interestedInZeroDPs = valueToRound * multiplicationFactor;

        return Math.round(interestedInZeroDPs) / multiplicationFactor;
    }
}
